package com.guigu.userservice.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.protobuf.ServiceException;
import com.guigu.userservice.dto.OperationLogDto;
import com.guigu.userservice.entity.OperationLog;
import com.guigu.userservice.mapper.OperationLogMapper;
import com.guigu.userservice.service.OperationLogService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * <p>
 * 操作日志表（记录操作员的所有系统操作，用于审计和问题排查） 服务实现类
 * </p>
 *
 * @author zcy
 * @since 2025-09-28
 */
@Service
public class OperationLogServiceImpl extends ServiceImpl<OperationLogMapper, OperationLog> implements OperationLogService {
    @Autowired
    private OperationLogMapper operationLogMapper;

    @Override
    public void insertOperationLog(OperationLogDto dto) throws ServiceException {
        OperationLog operationLog = new OperationLog();
        operationLog.setOperationTime(LocalDateTime.now());
        operationLog.setCreateTime(LocalDateTime.now());

        BeanUtils.copyProperties(dto, operationLog);

        // 添加空值检查
        if (operationLog.getOperatorId() == null) {
            throw new ServiceException("操作员ID不能为空");
        }

        if (operationLog.getOperationType() == null) {
            throw new ServiceException("操作类型不能为空");
        }

        operationLogMapper.insert(operationLog);
    }
}
